package com.example.community.entity;

/**
 * @Author: huang
 * @Date: 2021/9/14 13:27
 * @Description:    封装分页相关主件
 */
public class Page {

    //当前页码
    private int current = 1;
    //显示一页最多能有多少帖子
    private int limit = 10;
    //数据总数（用来计算总的页数，要知道一共有多少页）
    private int rows;
    //查询路径(第1页，第2页等等，这是一个可电机的跳转链接）
    private String path;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current >= 1) {
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit >= 1 && limit <= 100) {
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if (rows >= 0) {
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     *  获取当前页的起始行
     *  current * limit - limit = (current - 1) * limit
     */
    public int getOffset() {
        return (current - 1) * limit;
    }

    /**
     *  获取总的页数
     *
     */
    public int getTotal() {
        //rows / limit; 可能有余数，判断一下
        if (rows != 0 && rows % limit == 0 ) {
            return rows / limit;
        }else {
            return rows / limit + 1;
        }
    }

    /**
     * 页码如：1 ... 3 4 5 6 7 8... 12,当我们在第6页时，就显示它前面和后面两页
     * @return
     */
    public int getFrom() {
        int from = current - 2;
        return from < 1 ? 1 : from;
    }

    public int getTo() {
        int to = current + 2;
        int total = getTotal();
        return to > total ? total : to;
    }

}
